package com.wonders.module.auth.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wonders.framework.entity.vo.Page;
import com.wonders.framework.util.bean.BeanUtil;
import com.wonders.module.auth.dao.RoleDao;
import com.wonders.module.auth.dao.UserDao;
import com.wonders.module.auth.dao.UserRoleDao;
import com.wonders.module.auth.entity.bo.UserRole;
import com.wonders.module.auth.entity.vo.RoleVo;
import com.wonders.module.auth.entity.vo.UserRoleVo;
import com.wonders.module.auth.entity.vo.UserVo;
import com.wonders.module.auth.service.UserRoleService;

/**
 * 用户角色业务层实现
 * 
 * @author WuBin
 * 
 */
@Service
public class UserRoleServiceImpl implements UserRoleService {

	@Autowired
	private UserDao userDao;

	@Autowired
	private UserRoleDao userRoleDao;

	@Autowired
	private RoleDao roleDao;

	public Page queryRoleUser(UserRoleVo userRoleVo, Page page) {
		page = userDao.queryRoleUser(userRoleVo, page);
		return BeanUtil.copyPage(page, UserVo.class);
	}

	public void addRoleUser(Long roleId, List<Long> keys) {
		userRoleDao.removeRoleUser(roleId, keys);
		for (Long key : keys) {
			UserRole userRole = new UserRole();
			userRole.setRoleId(roleId);
			userRole.setUserId(key);
			userRoleDao.save(userRole);
		}
	}

	public void removeRoleUser(Long roleId, List<Long> keys) {
		userRoleDao.removeRoleUser(roleId, keys);
	}

	public Page queryUserRole(UserRoleVo userRoleVo, Page page) {
		page = roleDao.queryUserRole(userRoleVo, page);
		return BeanUtil.copyPage(page, RoleVo.class);
	}

	public void addUserRole(Long userId, List<Long> keys) {
		userRoleDao.removeUserRole(userId, keys);
		for (Long key : keys) {
			UserRole userRole = new UserRole();
			userRole.setUserId(userId);
			userRole.setRoleId(key);
			userRoleDao.save(userRole);
		}
	}

	public void removeUserRole(Long userId, List<Long> keys) {
		userRoleDao.removeUserRole(userId, keys);
	}

}
